package fr.pud.client.view;

import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.util.HashSet;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import fr.pud.client.controller.address.AddAddressToDatabase;
import fr.pud.client.controller.address.GoToAddressButton;
import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.model.Address;

/**
 * Apr 9, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         SearchFrame.java
 *         This class creates a frame which display all the address results get
 *         by
 *         nominatim during a user search
 */
public class SearchFrame extends JFrame {
    private static final long serialVersionUID = -4302566612455069500L;
    /**
     * A list of address to display
     */
    private HashSet<Address>  addressList;

    /**
     * Create the frame with all the addresses of the list
     * @param results
     */
    public SearchFrame(HashSet<Address> results) {
        super(LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.TITLE_SEARCH_FRAME));
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.addressList = results;
        this.setVisible(true);
        this.prepareLayout();
        this.pack();
        this.setLocation((int) Toolkit.getDefaultToolkit().getScreenSize()
                .getWidth()
                - this.getWidth(), 100);
    }

    /**
     * @return the list of addresses
     */
    public HashSet<Address> getListAddress() {
        return this.addressList;
    }

    /**
     * Add all the addresses into panel
     */
    private void prepareLayout() {
        this.getListAddress().size();
        this.getContentPane().setLayout(
                new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        for (Address address : this.getListAddress()) {
            JPanel pan = new JPanel(new FlowLayout());
            JPanel panForButton = new JPanel();
            panForButton
                    .setLayout(new BoxLayout(panForButton, BoxLayout.Y_AXIS));
            panForButton.add(new JButton(new GoToAddressButton(address
                    .getCoordinate())));
            panForButton.add(new JButton(new AddAddressToDatabase(address)));
            JLabel text = new JLabel();
            text.setText(address.toString());
            pan.add(panForButton);
            JScrollPane scroll = new JScrollPane(text);
            scroll.setOpaque(true);
            pan.add(scroll);
            this.getContentPane().add(pan);
        }
    }
}
